<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
        Object.create(prototype, [descriptors])
        以一个对象为原型，
        创建一个新的对象，并且对这个对象属性进行描述
     -->
    <script>

        // var val = 18;
        var newObj = Object.create({},{
            name:{
                value:"laowang",
                writable:true,
                configurable:true,
                enumerable:true,
            },
            age:{
                get:function(){
                    return this.val || 18;
                },
                set:function(v){
                    console.log(v);
                    this.val = v;
                }
            }
        })

         // console.log(newObj.name);

        // 触发的get函数
        // console.log(newObj.age);

        // // 触发set函数
        // newObj.age = 60;

        // console.log(newObj.age);

        // console.log(newObj);

            // =========================================

        // 闭包写法
        function newObjFn() {
            var val = 18;
            return Object.create({}, {
                name: {
                    value: "laowang",
                    writable: true,
                    configurable: true,
                    enumerable: true,
                },
                age: {
                    get: function () {
                        return val || 18;
                    },
                    set: function (v) {
                        val = v;
                    },
                    configurable:true,
                    enumerable:true
                }
            })
            // return newObj;
        }
       

        // 闭包 - 在函数内部生成一个局部变量存储空间
        var obj = newObjFn();

        obj.age = 66;

        // delete obj.age;

        console.log(obj);



    </script>

</body>

</html>